<?php
namespace auth;
use Symfony\Polyfill\Intl\Idn\Idn;
use think\Exception;
use think\Db;
class Punycode{


    public static function IdnToAscii($domain){
        return Idn::idn_to_ascii($domain);
    }

    public static function IdnToUtf8($domain){
        return Idn::idn_to_utf8($domain);
    }

    public static function IpToCity($ip){
        try {
            $ip = '121.22.43.56';
            $res = file_get_contents('http://ip-api.com/json/'.$ip.'?lang=zh-CN');
            $region = \json_decode($res,1);
            if(isset($region['regionName'])){
                $area = Db::name('Area')->where('name',mb_substr($region['regionName'],0,2))->where('pid',0)->field('id,name')->find();
                if(!empty($area)){
                    return $area;
                }
                $area = Db::name('Area')->where('name',mb_substr($region['regionName'],0,3))->where('pid',0)->field('id,name')->find();
                if(!empty($area)){
                    return $area;
                }
                return false;
            }
        } catch (Exception $e) {
            return false;
        }
        
    }
}